package com.rcloud.server.sealtalk.util.baidu;

import com.rcloud.server.sealtalk.constant.ConstantsURL;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 身份验证
 */
@Slf4j
@Component
public class PersonVerify {

    @Resource
    private AuthService authService;

    /**
     * 重要提示代码中所需工具类
     * FileUtil,Base64Util,HttpUtil,GsonUtils请从
     * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
     * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
     * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
     * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
     * 下载
     */
    public String personVerify(String image, String name, String idCardNumber, String clienId, String clientSecret) {
        try {
            //处理一下 Base64的图片数据
            if (image.contains("data:image/")) {
                image = (image.split("base64,")[1]);
            }
            Map<String, Object> map = new HashMap<>();
            map.put("image", image);
            map.put("image_type", "BASE64");
            map.put("name", name);
            map.put("id_card_number", idCardNumber);
            map.put("quality_control", "NORMAL");
            map.put("liveness_control", "NORMAL");
            String param = GsonUtils.toJson(map);
            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = authService.getAuth(clienId,clientSecret);
            String result = HttpUtil.post(ConstantsURL.personVerifyHost, accessToken, "application/json", param);
            log.info("百度云人脸活体检测------" + result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}